﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShopOnline.Models;
using ShopOnline.Models.DAL;

namespace ShopOnline.Controllers
{
    public class ContactController : Controller
    {
        private DAL_Contact contactRepository;
        //
        // GET: /Contact/

        public ContactController()
        {
            contactRepository = new DAL_Contact();
        }
         [Authorize]
        public ActionResult AdminListContact()
        {
            var list = contactRepository.getListContact();
            return View(list);
        }

        //
        // GET: /Contact/Details/5
         [Authorize]
        public ActionResult Details(int id)
        {
            var contact = contactRepository.getContactById(id);
            return View(contact);
        }

        //
        // GET: /Contact/Create
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Contact/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Contact contact = new Contact();
                contact.abbrev = collection["abbrev"];
                contact.address = collection["address"];
                contact.companyName = collection["companyName"];
                contact.email = collection["email"];
                contact.fax = collection["fax"];
                contact.phone = collection["phone"];
                contact.website = collection["website"];
                contactRepository.CreateContact(contact);
                return RedirectToAction("AdminListContact");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Contact/Edit/5
  [Authorize]
        public ActionResult Edit(int id)
        {
            var contact = contactRepository.getContactById(id);
            return View(contact);
        }

        //
        // POST: /Contact/Edit/5
 [Authorize]
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                Contact contact = contactRepository.getContactById(id);
                contact.abbrev = collection["abbrev"];
                contact.address = collection["address"];
                contact.companyName = collection["companyName"];
                contact.email = collection["email"];
                contact.fax = collection["fax"];
                contact.phone = collection["phone"];
                contact.website = collection["website"];
                contactRepository.EditContact(contact);
                return RedirectToAction("AdminListContact");
            }
            catch
            {
                var contact = contactRepository.getContactById(id);
                return View(contact);
            }
        }

        //
        // GET: /Contact/Delete/5
  [Authorize]
        public ActionResult Delete(int id)
        {
            var contact = contactRepository.getContactById(id);
            return View(contact);
        }

        //
        // POST: /Contact/Delete/5
 [Authorize]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                var contact = contactRepository.getContactById(id);
                contactRepository.DeleteContact(contact);
                return RedirectToAction("AdminListContact");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult contact()
        {
            var contact = contactRepository.getContact();
            return View(contact);
        }
        public ActionResult Info610UserControl()
        {
            var contact = contactRepository.getContact();
            return View(contact);
        }
       
    }
}
